package net.sxnic.ugr.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import net.sxinfo.core.dao.hibernate3.HibernateCriteria;
import net.sxinfo.core.dao.hibernate3.HibernateDaoUtils;
import net.sxinfo.core.dao.hibernate3.HibernateOrder;
import net.sxinfo.core.spring25.Dao;
import net.sxinfo.core.spring25.DefaultManager;
import net.sxnic.ugr.DwType;
import net.sxnic.ugr.dept.Dept;
import net.sxnic.ugr.dept.dao.DeptDao;
import net.sxnic.ugr.group.Group;
import net.sxnic.ugr.group.dao.GroupDao;
import net.sxnic.ugr.role.Role;
import net.sxnic.ugr.user.dao.UserDao;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("userManager")
public class DefaultUserManager extends DefaultManager<User, UserDao, String>
		implements UserManager {

	@Autowired
	private UserDao userDao;

	@Autowired
	private DeptDao deptDao;

	@Autowired
	private GroupDao groupDao;

	@Override
	protected Dao<User, String> getEntityDao() {
		return userDao;
	}

	/**
	 * @see net.sxinfo.application.user.UserManager#getUserByUsername(java.lang.String)
	 */
	public User getUserByUsername(String username) {
		return userDao.findByUnique("username", username);
	}

	/**
	 * @see net.sxinfo.application.user.UserManager#getOrderedUsers()
	 */
	public List<User> getOrderedUsersExceptAdmin() {
		List<User> userlist = userDao.getAll();

		List<User> newuserlist = new ArrayList<User>();
		for (User u : userlist) {
			if (!u.getUsername().equals("admin")) {
				newuserlist.add(u);

			}
		}
		return newuserlist;
	}

	public Map<String, String> findUserMap() {
		Map<String, String> map = new LinkedHashMap<String, String>();

		List<User> list = userDao.getAll();
		for (User user : list) {
			map.put(user.getUsername(), user.getFullname());
		}

		return map;
	}

	public Map<String, String> findUserOrderMap() {
		List<User> users = userDao.getAll();
		Map<String, String> map = new HashMap<String, String>();
		for (User u : users) {
			map.put(u.getUsername(), String.valueOf(u.getUserOrder()));
		}

		return map;
	}

	public Map<String, String> findOrderGroupUserNameMap(String deptCode) {
		Map<String, String> map = new HashMap<String, String>();
		Dept dept = deptDao.findByUnique("code", deptCode);
		for (User u : dept.getUsers()) {
			map.put(u.getUsername(), u.getFullname());
		}
		return map;
	}

	public int findMaxUserOrder() {
		return userDao.findMaxUserOrder();
	}

	@Override
	public List<User> getUserInGroup(Group group) {
		return userDao.getUserInGroup(group);
	}

	@Override
	public List<User> getUserInRole(Role role) {
		return userDao.getUserInRole(role);
	}

	@Override
	public User getChiefByUsername(String userName) {
		List<User> list = getAllDeptUsersByUsername(userName);
		if (list != null && list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	@Override
	public List<User> getAllDeptUsersByUsername(String userName) {
		User user = userDao.findByUnique("username", userName);
		if (user == null) {
			return null;
		}

		return userDao
				.findBySql(
						"from User where dept =? order by userOrder",
						new Object[] { user.getDept() });
	}

	@Override
	public User findGroupLeaderByDept(String deptCode) {
		List<User> list = userDao.findBySql(
				"from User where deptCodes =? order by userOrder",
				new Object[] { deptCode });
		if (list != null && list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	@Override
	public List<User> getAllLeaders() {
		Group group = groupDao.findByUnique("group", "0000");
		return getUserInGroup(group);
	}

	@Override
	public Dept getDeptByUser(String userName) {
		User user = this.getUserByUsername(userName);
		if(user ==null || user.getDept()==null){
			return null;
		}
		
		return user.getDept();
	}

	@Override
	public List<User> findAllChiefList() {
		return getAll();
	}

	@Override
	public List<User> findZbkhAllChiefList() {

		return getAll();
	}

	@Override
	public List<User> getAllStaffByDept(Dept dept) {
		
		HibernateOrder ho = HibernateDaoUtils.createHibernateOrder("userOrder", true);
		HibernateCriteria hc = new HibernateCriteria().add(ho);
		
		Criterion dc = Restrictions.eq("deptCodes", dept.getCode());
		hc.add(dc);
		
		return getByCriteria(hc);
	}
	
    /**
     * 根据用户全称和用户类型获取用户实体
     * @param fullname 用户全称
     * @param dwType 枚举类型单位类别
     * @return 对应的实体
     */
    public User getUserByFullname(String fullname,DwType dwType){
    	return userDao.getUserByFullname(fullname,dwType);
    }
    
    /**
     * 根据类别获取所有用户
     * @return 某一类别的所有用户列表
     */
    public List<User> findAllByDwType(DwType dwType)
    {
    	return userDao.findAllByDwType(dwType);
    }
    
    /**
     * 根据单位类别获取用户最大编码
     * @param dwType 单位类别
     * @return 指定类别的用户最大编码
     */
    public String findMaxByDwType(DwType dwType)
    {
    	return userDao.findMaxByDwType(dwType);
    }
    
	/**
	 * 查询某一类别的所有用户的map
	 * @return
	 */
	public Map<String,String> findMap(DwType dwType)
	{
		Map<String, String> map = new LinkedHashMap<String, String>();	
		List<User> list = userDao.findAllByDwType(dwType);

		for (User user : list) {
			map.put(user.getId(), user.getFullname());
		}
		
		return map;
	}
	
    /**
     * 根据用户名前缀获取该类用户的最大编码
     * @param userPrefix 用户名前缀
     * @return 指定前缀的用户最大类别
     */ 
    public String findMaxByUserPrefix(String userPrefix)
    {
    	return userDao.findMaxByUserPrefix(userPrefix);
    }
}
